home *** CD-ROM | disk | FTP | other *** search
/ Nikkei Mac 20 / NIKKEI-MAC-CD-VOL-20-1998-12.ISO.7z / NIKKEI-MAC-CD-VOL-20-1998-12.ISO / オンラインソフト / 9.ウェブ作成ツール / PageSpinner / PageSpinner Docs Japan.sit / PageSpinner Docs Japan / Examples / JavaScript / Cookie Example < prev    next >
Text File  |  1997-06-14  |  9KB  |  247 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>JavaScript Cookieのデモ</TITLE>
  4.  
  5. <SCRIPT>
  6. <!--
  7. /* 
  8.     Cookies Demo
  9.     By Jerry Aman, Optima System, July 28, 1996.
  10.        Cookie Functions written by Bill Dortch, hIdaho Design.
  11.  
  12.     Part of the PageSpinner distribution.
  13.  
  14.     We will not be held responsible for any unwanted 
  15.     effects due to the usage of this script or any derivative.  
  16.     No warrantees for usability for any specific application 
  17.     are given or implied.
  18.  
  19.     You are free to use and modify this script,
  20.     if credits are kept in the source code
  21. */
  22.  
  23.     var cookieVisitID     = 'my_LastVisit';    
  24.     var cookieNumVisitID     = 'my_NumVisits';    
  25.  
  26.                         // Change 'my_LastVisit' and 'my_NumVisits'
  27.                         // to something unique for
  28.                         // the current page, or else it
  29.                         // won't work correctly if other uses
  30.                         // this script!
  31.     var gLastVisit;
  32.     var gNumVisits;
  33.  
  34.     SetLastVisit();    // Execute when loading page
  35.  
  36.     function GetLastVisit ()
  37.     {
  38.  
  39.         if ( gLastVisit == "")
  40.         {
  41.             return "このサイトにようこそ!";
  42.         }
  43.         else 
  44.         {
  45.             var oldVisitDate = new Date(gLastVisit);
  46.             return     "またお会いしましたね、前回お会いしたのは " 
  47.                     + gLastVisit +"です<BR>これまで、" 
  48.                     + gNumVisits + "回 "  
  49.                     +"来ていただきましたね"
  50.         }
  51.     }
  52.  
  53.  
  54.     function SetLastVisit (name, value) 
  55.     {
  56.         var newVisitDate = new Date();
  57.         var expDate = new Date (); 
  58.         var numVisits = 0;
  59.  
  60.             // The expDate is the date when the cookie should
  61.             // expire, we will keep it for a year
  62.         expDate.setTime( expDate.getTime() + (365 * 24 * 60 * 60 * 1000) ); 
  63.  
  64.             // Info about last visit
  65.         if (GetCookie (cookieVisitID) != null)
  66.             gLastVisit = GetCookie (cookieVisitID);
  67.         else
  68.             gLastVisit = "";
  69.  
  70.         if (GetCookie (cookieNumVisitID) != null)
  71.             gNumVisits = GetCookie (cookieNumVisitID);    
  72.         else
  73.             gNumVisits = 0;
  74.  
  75.             // Use eval() to convert a string to a number
  76.         numVisits = eval(gNumVisits) +1;    
  77.  
  78.             // Store info about this visit
  79.         SetCookie( cookieVisitID,     newVisitDate, expDate); 
  80.         SetCookie( cookieNumVisitID, numVisits, expDate); 
  81.     }
  82.  
  83.     // ---------------------------------------------------------------
  84.     //  Cookie Functions - Second Helping  (21-Jan-96)
  85.     //  Written by:  Bill Dortch, hIdaho Design <BDORTCH@NETW.COM>
  86.     //  The following functions are released to the public domain.
  87.     //
  88.     //  The Second Helping version of the cookie functions dispenses with
  89.     //  my encode and decode functions, in favor of JavaScript's new built-in
  90.     //  escape and unescape functions, which do more complete encoding, and
  91.     //  which are probably much faster.
  92.     //
  93.     //  The new version also extends the SetCookie function, though in
  94.     //  a backward-compatible manner, so if you used the First Helping of
  95.     //  cookie functions as they were written, you will not need to change any
  96.     //  code, unless you want to take advantage of the new capabilities.
  97.     //
  98.     //  The following changes were made to SetCookie:
  99.     //
  100.     //  1.  The expires parameter is now optional - that is, you can omit
  101.     //      it instead of passing it null to expire the cookie at the end
  102.     //      of the current session.
  103.     //
  104.     //  2.  An optional path parameter has been added.
  105.     //
  106.     //  3.  An optional domain parameter has been added.
  107.     //
  108.     //  4.  An optional secure parameter has been added.
  109.     //
  110.     //  For information on the significance of these parameters, and
  111.     //  and on cookies in general, please refer to the official cookie
  112.     //  spec, at:
  113.     //
  114.     //      http://www.netscape.com/newsref/std/cookie_spec.html    
  115.     //
  116.     //
  117.     // "Internal" function to return the decoded value of a cookie
  118.     //
  119.     function getCookieVal (offset) {
  120.       var endstr = document.cookie.indexOf (";", offset);
  121.       if (endstr == -1)
  122.         endstr = document.cookie.length;
  123.       return unescape(document.cookie.substring(offset, endstr));
  124.     }
  125.  
  126.     //
  127.     //  Function to return the value of the cookie specified by "name".
  128.     //    name - String object containing the cookie name.
  129.     //    returns - String object containing the cookie value, or null if
  130.     //      the cookie does not exist.
  131.     //
  132.     function GetCookie (name) {
  133.       var arg = name + "=";
  134.       var alen = arg.length;
  135.       var clen = document.cookie.length;
  136.       var i = 0;
  137.       while (i < clen) {
  138.         var j = i + alen;
  139.         if (document.cookie.substring(i, j) == arg)
  140.           return getCookieVal (j);
  141.         i = document.cookie.indexOf(" ", i) + 1;
  142.         if (i == 0) break; 
  143.       }
  144.       return null;
  145.     }
  146.  
  147.     //
  148.     //  Function to create or update a cookie.
  149.     //    name - String object object containing the cookie name.
  150.     //    value - String object containing the cookie value.  May contain
  151.     //      any valid string characters.
  152.     //    [expires] - Date object containing the expiration data of the cookie.  If
  153.     //      omitted or null, expires the cookie at the end of the current session.
  154.     //    [path] - String object indicating the path for which the cookie is valid.
  155.     //      If omitted or null, uses the path of the calling document.
  156.     //    [domain] - String object indicating the domain for which the cookie is
  157.     //      valid.  If omitted or null, uses the domain of the calling document.
  158.     //    [secure] - Boolean (true/false) value indicating whether cookie transmission
  159.     //      requires a secure channel (HTTPS).  
  160.     //
  161.     //  The first two parameters are required.  The others, if supplied, must
  162.     //  be passed in the order listed above.  To omit an unused optional field,
  163.     //  use null as a place holder.  For example, to call SetCookie using name,
  164.     //  value and path, you would code:
  165.     //
  166.     //      SetCookie ("myCookieName", "myCookieValue", null, "/");
  167.     //
  168.     //  Note that trailing omitted parameters do not require a placeholder.
  169.     //
  170.     //  To set a secure cookie for path "/myPath", that expires after the
  171.     //  current session, you might code:
  172.     //
  173.     //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  174.     //
  175.     function SetCookie (name, value) {
  176.       var argv = SetCookie.arguments;
  177.       var argc = SetCookie.arguments.length;
  178.       var expires = (argc > 2) ? argv[2] : null;
  179.       var path = (argc > 3) ? argv[3] : null;
  180.       var domain = (argc > 4) ? argv[4] : null;
  181.       var secure = (argc > 5) ? argv[5] : false;
  182.       document.cookie = name + "=" + escape (value) +
  183.         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  184.         ((path == null) ? "" : ("; path=" + path)) +
  185.         ((domain == null) ? "" : ("; domain=" + domain)) +
  186.         ((secure == true) ? "; secure" : "");
  187.     }
  188.  
  189.     //  Function to delete a cookie. (Sets expiration date to current date/time)
  190.     //    name - String object containing the cookie name
  191.     //
  192.     function DeleteCookie (name) {
  193.       var exp = new Date();
  194.       exp.setTime (exp.getTime() - 1);  // This cookie is history
  195.       var cval = GetCookie (name);
  196.       document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  197.     }
  198.  
  199. // -->
  200. </SCRIPT>
  201.  
  202. </HEAD>
  203.  
  204. <BODY BGCOLOR=FFFFFF TEXT=000000>
  205.  
  206. <H1>JavaScript Cookieの例</H1>
  207.  
  208. <B>このページには、前回読者が訪れたページの情報を保存するJavaScriptが含まれています。</B>
  209. <P>
  210. 現在 JavaScript は Netscape Navigator 2.0 以降と MS Internet Explorer 3.0以降でのみ使用できることに注意して下さい。<BR>
  211. <FONT COLOR="931B15">あなたはすべての読者が JavaScript を埋め込まれたブラウザを使用していると思ってはいけません。</FONT>
  212. <HR>
  213. <P>
  214. これらのJavaScripts では Persistent Client State HTTP Cookies を使用して、前回に読者が訪れたページの情報を保存します。クッキーは複数のセッションにわたって情報を保存できる機構です。
  215. <P>
  216. この例では2つのクッキーが保存され、前日の情報と読者がページを訪れた回数が記録されています。クッキーは読者のハードディスクに保存されるものなので、ページの制作者側ではその読者の情報を引出すことはできません。
  217. <P>
  218. クッキーは、その他にも多くの目的で利用されます。そのサイトを見る読者のパフォーマンスを最適に維持したり、パスワードを保存したり、オンラインビジネスでショッピングカートを使えるようにしたり、アクセス権を監視して特定の読者がページを見ながら同時にサーバーに記録を残したりできます。
  219. <HR>
  220. <B>クッキーの例を示します:</B>
  221.  
  222. <P>
  223. こんにちは <SCRIPT>document.write(navigator.userAgent);</SCRIPT>を使っていますね!
  224.  
  225. <P>
  226. <SCRIPT>document.write (GetLastVisit())</SCRIPT>
  227.  
  228. <P>
  229. このページをリロードして上記のテキストが更新されるのを見ましょう。
  230.  
  231. <P>
  232. <HR>
  233. <B>使い方:</B><BR>
  234. <CODE>'my_LastVisit'</CODE> と '<CODE>my_NumVisits'</CODE> の名前をリネームして何かこのページでユニークな名前(urlの一部を使うとよい)に変更して下さい。そうしておかないと、他のユーザがこのスクリプトを使っている場合に正しく働きません。これらの定数はこのファイルのHEADセクションの最初に置きます。ここではそうしておくことが重要です。
  235.  
  236. <P>
  237. <CODE>GetLastVisit()</CODE> 関数の中にあるテキストを変更して、なにかもっとあなたのサイトにふさわしいものに変更することもできます。
  238.  
  239. <P>
  240. 最後に BODY セクションの中で、次のコードを使い、ブラウザに情報を表示します:
  241. <XMP><SCRIPT>document.write (GetLastVisit())</SCRIPT></XMP>
  242.  
  243. <P>
  244. <!--Translated by <A HREF="mailto:hosoka@ca2.so-net.or.jp">Shuji HOSOKAWA</A>-->
  245. </BODY>
  246. </HTML>
  247.